package com.tuyrt.windy.logic.network

import retrofit2.await

/**
 * Created by tuyrt7 on 2021/4/14.
 */
object WindyWeatherNetwork {

    private val placeService = ServiceCreator.create<PlaceService>()
    private val weatherService = ServiceCreator.create<WeatherService>()

    suspend fun searchPlaces(query: String) = placeService.searchPlaces(query).await()

    suspend fun getDaily(lat: String, lng: String) = weatherService.getDaily(lat, lng).await()
    suspend fun getRealtime(lat: String, lng: String) = weatherService.getRealtime(lat, lng).await()

//    private suspend fun <T> Call<T>.await():T {
//        return suspendCoroutine {
//            continuation ->
//            enqueue(object :Callback<T> {
//                override fun onResponse(call: Call<T>, response: Response<T>) {
//                    response.takeIf { response.isSuccessful }
//                        ?.body()?.let { continuation.resume(it) }
//                        ?: continuation.resumeWithException(RuntimeException("response body is null."))
//                }
//
//                override fun onFailure(call: Call<T>, t: Throwable) {
//                    continuation.resumeWithException(t)
//                }
//            })
//        }
//    }
}


